package cn.stylefeng.guns.modular.system.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.system.entity.DealerLevel;
import cn.stylefeng.guns.modular.system.mapper.DealerLevelMapper;
import cn.stylefeng.guns.modular.system.model.params.DealerLevelParam;
import cn.stylefeng.guns.modular.system.model.result.DealerLevelResult;
import cn.stylefeng.guns.modular.system.service.AddressRoleService;
import cn.stylefeng.guns.modular.system.service.AddressService;
import  cn.stylefeng.guns.modular.system.service.DealerLevelService;
import cn.stylefeng.guns.sys.core.util.utility.Katrina_CJ_Utils;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 经销商等级表 服务实现类
 * </p>
 *
 * @author 小飞
 * @since 2019-06-27
 */
@Service
public class DealerLevelServiceImpl extends ServiceImpl<DealerLevelMapper, DealerLevel> implements DealerLevelService {

    private static final Joiner JOINER = Joiner.on("").skipNulls();
    @Autowired
    private AddressService  addressService;
    @Override
    public void add(DealerLevelParam param){
        DealerLevel entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(DealerLevelParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(DealerLevelParam param){
        DealerLevel oldEntity = getOldEntity(param);
        DealerLevel newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public DealerLevelResult findBySpec(DealerLevelParam param){
        return null;
    }

    @Override
    public List<DealerLevelResult> findListBySpec(DealerLevelParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(DealerLevelParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        List<DealerLevelResult> dealerLevelResults = page.getRecords();
        dealerLevelResults.stream().filter(s -> ToolUtil.isNotEmpty(addressService.getById(s.getAreaId()))).forEach(s -> {
           Long areaId = s.getAreaId();
           if (ToolUtil.isNotEmpty(areaId)){
               String nameOne = addressService.getById(areaId).getName();
               String nameTwo = null;
               String nameThree  = null;
               if (addressService.getById(areaId).getPid() != 0L){
                   nameTwo = addressService.getById(addressService.getById(areaId).getPid()).getName();
                   if (addressService.getById(addressService.getById(areaId).getPid()).getPid() != 0L){
                       nameThree = addressService.getById(addressService.getById(addressService.getById(areaId).getPid())).getName();
                   }
               }
               s.setArea(JOINER.join(Lists.newArrayList(nameThree,nameTwo,nameOne)));
           }
        });
        page.setRecords(dealerLevelResults);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(DealerLevelParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private DealerLevel getOldEntity(DealerLevelParam param) {
        return this.getById(getKey(param));
    }

    private DealerLevel getEntity(DealerLevelParam param) {
        DealerLevel entity = new DealerLevel();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
